package org.lab.jiang;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.shirdrn.document.preprocessing.api.Term;
import org.shirdrn.document.preprocessing.api.TermFilter;

import util.ReflectionUtils;


public abstract class AbstractWechatAnalyzer implements WechatRecordAnalyzer {

	private static final Log LOG = LogFactory.getLog(AbstractWechatAnalyzer.class);
	protected String charSet = "UTF-8";
	private final static Set<TermFilter> filters = new HashSet<TermFilter>();
	private final Set<String> stopwords = new HashSet<String>();
	
	
    
	public AbstractWechatAnalyzer() {
		load();
		FiltersSelect();
		
	}
	
    private void FiltersSelect(){
  	String[] aClazz = Config.DOCUMENT_FILTER_CLASSES.split(",");
	 for(String clazz : aClazz){
	 	TermFilter filter = ReflectionUtils.newInstance(clazz,TermFilter.class);
 		filters.add(filter);
		}			
     }
	
	public Map<String, Term> getWordMap(String content){
		Map<String, Term> result = analyze(content);
		for(TermFilter filter : filters) {
			filter.filter(result);
		}
		return result;
	}
	protected abstract Map<String, Term> analyze(String content) ;
	
	protected boolean isStopword(String word) {
		if(word != null) {
			word = word.trim();
			if(word.isEmpty()) {
				return true;
			} else {
				return stopwords.contains(word);
			}
		}
		return true;
	}
	
	private void load() {
		InputStream in = AbstractWechatAnalyzer.class.getClassLoader().getResourceAsStream("stopwords.txt");
//		File file = new File("stopwords.txt");
//		System.out.println(file.getAbsolutePath());
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new InputStreamReader(in));
			String word = null;
			while((word = reader.readLine()) != null) {
				word = word.trim();
				if(!word.isEmpty()) {
					if(!stopwords.contains(word)) {
						stopwords.add(word);
						//System.out.println("word"+word);
					}
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}  catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}		
	}
}
